﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Indexer_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //  示例1 
        GetIndexer();
        //  示例2
        getWeedayName();
    }

    #region 示例1
    
    /*
示例1

说明:
        下面的示例说明如何声明私有数组字段、temps 和索引器。 使用索引器可直接访问实例 tempRecord[i]。 另一种使用索引器的方法是将数组声明为 public 成员并直接访问它的成员 tempRecord.temps[i]。

        请注意，当计算索引器的访问时（例如，在 Console.Write 语句中），将调用 get 访问器。 因此，如果 get 访问器不存在，将发生编译时错误。
     */
    class TempRecord
    {
        // Array of temperature values
        private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 
                                            61.3F, 65.9F, 62.1F, 59.2F, 57.5F };

        // To enable client code to validate input 
        // when accessing your indexer.
        public int Length
        {
            get { return temps.Length; }
        }
        // Indexer declaration.
        // If index is out of range, the temps array will throw the exception.
        public float this[int index]
        {
            get
            {
                return temps[index];
            }

            set
            {
                temps[index] = value;
            }
        }
    }

    class MainClass
    {
        public static void Main()
        {
            TempRecord tempRecord = new TempRecord();
            // Use the indexer's set accessor
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;

            // Use the indexer's get accessor
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
            }

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            //System.Console.ReadKey();

        }
    }

    public void GetIndexer()
    {
        TempRecord tempRecord = new TempRecord();
        // Use the indexer's set accessor
        tempRecord[3] = 58.3F;
        tempRecord[5] = 60.1F;

        StringBuilder sb = new StringBuilder();

        // Use the indexer's get accessor
        for (int i = 0; i < 10; i++)
        {
            sb.Append(string.Format("Element #{0} = {1}", i, tempRecord[i]));
            sb.Append("<br>");
        }
        Label1.Text = sb.ToString();
    }

    /* Output:
            Element #0 = 56.2
            Element #1 = 56.7
            Element #2 = 56.5
            Element #3 = 58.3
            Element #4 = 58.8
            Element #5 = 60.1
            Element #6 = 65.9
            Element #7 = 62.1
            Element #8 = 59.2
            Element #9 = 57.5
        */
    #endregion

    #region 示例2
    
    /*
示例2
 
     在此例中，声明了存储星期几的类。 声明了一个 get 访问器，它接受字符串（天名称），并返回相应的整数。 例如，星期日将返回 0，星期一将返回 1，等等。
     */

    // Using a string as an indexer value
    class DayCollection
    {
        string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

        // This method finds the day or returns -1
        private int GetDay(string testDay)
        {

            for (int j = 0; j < days.Length; j++)
            {
                if (days[j] == testDay)
                {
                    return j;
                }
            }

            throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form \"Sun\", \"Mon\", etc");
        }

        // The get accessor returns an integer for a given string
        public int this[string day]
        {
            get
            {
                return (GetDay(day));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DayCollection week = new DayCollection();
            System.Console.WriteLine(week["Fri"]);

            // Raises ArgumentOutOfRangeException
            System.Console.WriteLine(week["Made-up Day"]);

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    // Output: 5

    public void getWeedayName()
    {
        DayCollection week = new DayCollection();
        StringBuilder sb = new StringBuilder();

        sb.Append(week["Fri"]); sb.Append("<br>");
        // Raises ArgumentOutOfRangeException
        //sb.Append(week["Made-up Day"]); sb.Append("<br>");

        Label2.Text = sb.ToString();
    }

    #endregion

}